<script setup>
import {
    Management,
    Promotion,
    UserFilled,
    User,
    Crop,
    EditPen,
    SwitchButton,
    CaretBottom
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'

//导入获取用户信息的接口
import { userInfoService } from '@/api/user.js'
//导入store的创建用对象useUserInfoStore，要与userinfo.js中导出的对象名一样
import useUserInfoStore from '@/stores/userInfo.js'
//获取store对象
const userInfoStore = useUserInfoStore()

const getuserInfo = async () => {
    let result = await userInfoService()
    //存储到store中，供多个vue使用,setUserInfo是在userinfo.js中定义的方法
    userInfoStore.setInfo(result.data)
}

//页面渲染时调用一下
getuserInfo()

   
//导入确认框
import { ElMessageBox,ElMessage } from 'element-plus'
//导入路由，用于切换顶栏菜单
import {useRouter} from 'vue-router'
//导入store中的token，退出登录时清空
import {useTokenStore} from '@/stores/token.js'
//获取token对象
const tokenStore = useTokenStore()
//获取路由对象
const router = useRouter()
// 处理点击菜单项时的事件，接收菜单项点击时传入的指令参数command
const handleCommand = (command) => {
    //判断指令（参数值），如果是logout，代表“退出登录”选项
    if (command === 'logout') {
        //退出登录,确认框确认一下
        ElMessageBox.confirm('确认退出登录吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '点错了',
            type: 'warning'
        }).then(() => {
            //点击确定后，清空store中的token和个人信息userinfo
            tokenStore.removeToken()  //store中定义的方法
            userInfoStore.removeInfo()
            localStorage.removeItem('token')  //浏览器的本地存储中删除token，这个参数的命名时浏览器固定的
            //跳转到登录页面
            router.push('/login')
            //提示
            ElMessage.success('退出登录成功')
        }).catch(() => {
            //点击取消后，提示
            ElMessage.info('已取消退出')
        })
    } else {
        //切换路由.push方法切换路由，参数就是传入的指令（参数没有/，所以要补充一个）
        router.push('/user/'+command)
    }
}

//


</script>



<template>
    <!-- el-container是element-plus中的容器相当于div -->
    <el-container class="layout-container">
        <!--el-aside 左侧菜单 -->
        <el-aside width="200px">
            <div class="el-aside__logo"></div>
<!--el-menu是element-plus的菜单标签   el-menu-item是菜单项   ，，，index属性配置点击菜单项时去访问哪个路由路径  <Management />这些标签只是图标-->
            <el-menu active-text-color="#ffd04b" background-color="#232323"  text-color="#fff"
                router>
                <el-menu-item index="/article/category">
                    <el-icon>
                        <Management />
                    </el-icon>
                    <span>文章分类</span>
                </el-menu-item>
                <el-menu-item index="/article/manage">
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>文章管理</span>
                </el-menu-item>
                <!-- el-sub-menu子菜单 ，配置子页面标签-->
                <el-sub-menu >
                    <template #title>
                        <el-icon>
                            <UserFilled />
                        </el-icon>
                        <span>个人中心</span>
                    </template>
                    <el-menu-item index="/user/info">
                        <el-icon>
                            <User />
                        </el-icon>
                        <span>基本资料</span>
                    </el-menu-item>
                    <el-menu-item index="/user/avatar">
                        <el-icon>
                            <Crop />
                        </el-icon>
                        <span>更换头像</span>
                    </el-menu-item>
                    <el-menu-item index="/user/resetPassword">
                        <el-icon>
                            <EditPen />
                        </el-icon>
                        <span>重置密码</span>
                    </el-menu-item>
                </el-sub-menu>
            </el-menu>
        </el-aside>
        <!-- 右侧主区域 el-container容器-->
        <el-container>
            <!-- el-header头部区域 -->

            <el-header>
                <!-- 个人信息，从store中获取 -->
                <div>欢迎您，用户：<strong>{{ userInfoStore.info.nickname}}</strong></div>
                <!-- 头像框区域，el-dropdown可点击的下拉区域 -->
                <!-- command: 条目被点击后会触发,在事件函数上可以声明一个参数,接收条目对应的指令 -->
                <el-dropdown placement="bottom-end" @command="handleCommand">
                    <span class="el-dropdown__box">
                        <!-- el-avatar圆形头像框，src配置路径,avater时导入的默认图片 -->
                        <el-avatar :src="userInfoStore.info.userPic? userInfoStore.info.userPic : avatar" />
                        <el-icon>
                            <CaretBottom />
                        </el-icon>
                    </span>
                    <template #dropdown>
                        <!-- el-dropdown-menu下拉菜单 ,command属性声明一个参数作为指令，将这个参数传给处理方法handleCommand-->
                        <el-dropdown-menu>
                            <el-dropdown-item command="info" :icon="User">基本资料</el-dropdown-item>
                            <el-dropdown-item command="avatar" :icon="Crop">更换头像</el-dropdown-item>
                            <el-dropdown-item command="resetPassword" :icon="EditPen">重置密码</el-dropdown-item>
                            <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </el-header>

            <!-- el-main主区域，中间区域 -->
            <el-main>
                <!-- 路由出口 -->
                <router-view></router-view>
            </el-main>

            <!--el-footer位于底部 底部区域 -->
            <el-footer>大事件 ©2025 Created by wangjj</el-footer>
        </el-container>
    </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
    height: 100vh;

    .el-aside {
        background-color: #232323;

        &__logo {
            height: 120px;
            background: url('@/assets/logo.png') no-repeat center / 120px auto;
        }

        .el-menu {
            border-right: none;
        }
    }

    .el-header {
        background-color: #fff;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .el-dropdown__box {
            display: flex;
            align-items: center;

            .el-icon {
                color: #999;
                margin-left: 10px;
            }

            &:active,
            &:focus {
                outline: none;
            }
        }
    }

    .el-footer {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        color: #666;
    }
}
</style>